/**
 * 
 */
package com.thor.palmmall.rest.patrol.repair;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.commons.util.Assert;
import com.thor.palmmall.rest.common.AAssignInfo;
import com.thor.palmmall.rest.common.ASignInInfo;
import com.thor.palmmall.rest.common.AUserGroupLoadParam;
import com.thor.palmmall.rest.common.AUserGroupQueryParam;
import com.thor.palmmall.rest.common.SAssignInfoConverter;
import com.thor.palmmall.rest.common.SSignInInfoConverter;
import com.thor.palmmall.rest.common.SUserGroupLoadParamConverter;
import com.thor.palmmall.rest.common.SUserGroupQueryParamConverter;
import com.thor.palmmall.rest.patrol.repair.converter.ADeviceMaintainInspectConverter;
import com.thor.palmmall.rest.patrol.repair.converter.SDeviceMaintain4CreateConverter;
import com.thor.palmmall.rest.patrol.repair.converter.SDeviceMaintainInspectConverter;
import com.thor.palmmall.spi.common.SOperateInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.device.maintain.SDeviceMaintainInspect;
import com.thor.palmmall.spi.device.maintain.SDeviceMaintainService;

/**
 * @author NuoSa
 *
 * @since 1.0
 */
public class APatrolRepairServiceImpl implements APatrolRepairService {

  @Override
  public String create(ADeviceMaintain4Create bill, BeanOperateInfo operInfo)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(bill, "bill");
    Assert.assertArgumentNotNull(operInfo, "operateInfo");
    Assert.assertArgumentNotNull(operInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operInfo.getTime(), "operateInfo.getTime()");
    SResponse<String> resp = sDeviceMaintainService.create(
        SDeviceMaintain4CreateConverter.getInstance().convert(bill),
        SOperateInfoConverter.getInstance().convert(operInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法创建设备维修单：" + resp.getMessage());
  }

  @Override
  public ADeviceMaintainInspect get(String id, AUserGroupLoadParam param)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(id, "id");
    SResponse<SDeviceMaintainInspect> resp = sDeviceMaintainService.get(id,
        SUserGroupLoadParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return ADeviceMaintainInspectConverter.getInstance().convert(resp.getBody());
    else
      throw new ThorServiceException("无法找到指定ID的设备报修单：" + resp.getMessage());
  }

  @Override
  public String save(ADeviceMaintainInspect bill, BeanOperateInfo operInfo)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(bill, "bill");
    Assert.assertArgumentNotNull(operInfo, "operateInfo");
    Assert.assertArgumentNotNull(operInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operInfo.getTime(), "operateInfo.getTime()");
    SResponse<String> resp = sDeviceMaintainService.save(
        SDeviceMaintainInspectConverter.getInstance().convert(bill),
        SOperateInfoConverter.getInstance().convert(operInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法保存设备维修单：" + resp.getMessage());
  }

  @Override
  public QueryResult<ADeviceMaintainInspect> query(AUserGroupQueryParam queryParam)
      throws ThorServiceException {
    SResponse<SQueryResult<SDeviceMaintainInspect>> resp = sDeviceMaintainService
        .query(SUserGroupQueryParamConverter.getInstance().convert(queryParam));
    if (resp.isSuccess())
      return SQueryResultConverter.convert(resp.getBody(),
          ADeviceMaintainInspectConverter.getInstance());
    else
      throw new ThorServiceException("无法查询设备维修单：" + resp.getMessage());
  }

  @Override
  public void signIn(ASignInInfo signInInfo) throws ThorServiceException {
    Assert.assertArgumentNotNull(signInInfo, "signInInfo");

    SResponse<Void> resp = sDeviceMaintainService
        .signIn(SSignInInfoConverter.getInstance().convert(signInInfo));
    if (resp.isSuccess())
      return;
    else
      throw new ThorServiceException("抢单失败：" + resp.getMessage());
  }

  @Override
  public void assign(AAssignInfo assignInfo) throws ThorServiceException {
    Assert.assertArgumentNotNull(assignInfo, "assignInfo");

    SResponse<Void> resp = sDeviceMaintainService
        .assign(SAssignInfoConverter.getInstance().convert(assignInfo));
    if (resp.isSuccess())
      return;
    else
      throw new ThorServiceException("指派维修单失败：" + resp.getMessage());
  }

  @Autowired
  private SDeviceMaintainService sDeviceMaintainService;
}
